// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var validate = require("validate");
	var bulletinId = base.getUrlParam("bulletinId");
	var css_param = require("./paramManage.css");
	var fileSrc = "";
	var fileSize = "";
	var fileName = "";
	var paramName = "";
	var paramId = "";
	var paramValue = "";
	var paramType = "";
	var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
	var init = function() {
		initEvent();
		getParamList();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		//修改顶部栏的选中状态
		$(".nav_hover", window.parent.document).removeClass("head_link_1");
		// 删除附件
		$("#attach_part").on("click", ".attach_delete", function() {
			$(this).parents("div.attach_list").remove();
		});
		
		//点击修改
		$("#systemParamList").on("click", ".mod_button", function() {
			$(this).addClass("none");
			$(this).parent().find(".update_button").removeClass("none");
			$(this).parent().parent().find(".paramValue").css("border","1px solid #e2eaea").removeAttr("readonly");
		});
		
		//修改系统参数
		$("#systemParamList").on("click", ".update_button", function() {
			paramId = $(this).parent().parent().attr("paramId");
			paramValue = $(this).parent().parent().find(".paramValue").val();
			paramType = $(this).parent().parent().attr("paramType");
			updateParam();
			$(this).addClass("none");
			$(this).parent().find(".mod_button").removeClass("none");
			$(this).parent().parent().find(".paramValue").css("border","none").attr("readonly", "readonly");
		});
		
		$("#systemParamList").on("change", "#uploadFile", function(){
			paramId = $(this).parent().attr("paramId");
			paramType = $(this).parent().attr("paramType");
			uploadFile();
		});
	};
	
	//上传文件
	var uploadFile = function() {
		if ($("#uploadFile").val() != "") {
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: 'uploadFile',
				dataType: 'json',
				success: function(data) {
					paramValue = data.data[0].fileUuid;
					updateParam();
				},
				complete: function(xml) {
					$("#uploadFile").change(function() {
						uploadFile();
					});
				},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			base.alert(3, "请选择文件！");
		}
	};
	
	//获取系统参数列表
	var getParamList = function(){
        base.fnAjax({
            action: "inquireSystemParamList.json",
            oData: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                paramName: paramName,
                paramType: 0
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                    var tpl = $("#systemParamListTpl").html();
                    var html = juicer(tpl, data);
                    $("#systemParamList").html(html);
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl($("#paramManage .pagerCon"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            getParamList();
                        });
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
        });
    };
    
    //修改系统参数
	var updateParam = function(){
        base.fnAjax({
            action: "updateSystemParam.json",
            oData: {
                paramValue: paramValue,
				paramId: paramId,
				paramType: paramType
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                    base.alert(1,"修改成功！");
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
        });
    };
	
	return {
		init: init
	};
});